package com.niubi.yygh.hosp.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.niubi.yygh.common.exception.YyghException;
import com.niubi.yygh.common.result.R;
import com.niubi.yygh.hosp.service.HospitalSetService;
import com.niubi.yygh.model.hosp.HospitalSet;
import com.niubi.yygh.vo.hosp.HospitalSetQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import util.HttpRequestHelper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * <p>
 * 医院设置表 前端控制器
 * </p>
 *
 * @author 牛俊杰
 * @since 2022-03-11
 */
//@CrossOrigin
@Api(description = "医院设置接口")
@RestController//返回json数据
@RequestMapping("/admin/hosp/hospitalset")

public class HospitalSetController {
  @Autowired
  HospitalSetService hospitalSetService;

  //1.查询所有记录
  @ApiOperation(value = "医院设置列表")
  @GetMapping("findAll")
  public R  findHospSetAll(){

//    try {
//      int i= 10/0;
//    } catch (Exception e) {
//      throw new YyghException(20001,"出现自定义异常");
//    }

    List<HospitalSet> list = hospitalSetService.list();

//    R r = new R();
//    r.setSuccess(true);
//    r.setCode(20000);
//    r.setMessage("成功");
//    HashMap<String, Object> data = new HashMap<>();
//    data.put("items",list);
//    r.data("list",list);
//    return r;

    return R.ok().data("list",list).message("调用成功");
  }

  //2.逻辑删除
  @ApiOperation(value = "医院设置删除")
  @DeleteMapping("remove/{id}")
  public R removeHospSet(@PathVariable Long id){
    boolean isSuccess = hospitalSetService.removeById(id);

    if (isSuccess){
      return R.ok().message("删除成功");
    }else {
      return R.error().message("删除失败");
    }
  }

  //3.分页查询
  @ApiOperation(value = "分页查询")
  @GetMapping("findPage/{current}/{limit}")
  public R findPage(@PathVariable long current,@PathVariable long limit){
    //创建page对象
    Page<HospitalSet> pageParam = new Page<>(current,limit);
    hospitalSetService.page(pageParam);
    long total = pageParam.getTotal();
    List<HospitalSet> list = pageParam.getRecords();
    return R.ok().data("total",total).data("rows",list);
  }

  //4.条件查询带分页
  @ApiOperation(value = "条件分页查询")
  @PostMapping("findQuery/{current}/{limit}")
  public R findQuery(@PathVariable long current, @PathVariable long limit ,@RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo){
    Page<HospitalSet> pageParam = new Page<>(current,limit);
    QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();
    if (hospitalSetQueryVo == null){
    hospitalSetService.page(pageParam);
    }else {
      String hosname = hospitalSetQueryVo.getHosname();
      String hoscode = hospitalSetQueryVo.getHoscode();
      if (!StringUtils.isEmpty(hosname)){
        wrapper.like("hosname",hosname);
      }
      if (!StringUtils.isEmpty(hoscode)){
        wrapper.eq("hoscode",hoscode);
      }
      hospitalSetService.page(pageParam,wrapper);
    }

    long total = pageParam.getTotal();
    List<HospitalSet> list = pageParam.getRecords();
    return R.ok().data("total",total).data("rows",list);
  }

  //5.添加
  @ApiOperation(value = "添加医院信息")
  @PostMapping("saveHospSet")
  public R saveHospSet(@RequestBody HospitalSet hospitalSet){
    String signKey = System.currentTimeMillis() + "" + new Random().nextInt(1000);
    hospitalSet.setSignKey(signKey);
    boolean isSuccess = hospitalSetService.save(hospitalSet);
    if (isSuccess){
      Map<String,Object> map = new HashMap<>();
      map.put("sign",signKey);
      map.put("hoscode",hospitalSet.getHoscode());
      HttpRequestHelper.sendRequest(map,"http://localhost:9998/hospSet/updateSignKey");
      return R.ok();
    }else {
      return R.error();
    }
  }

  //6.修改-查找
  @ApiOperation(value = "通过ID查询")
  @GetMapping("findHospSet/{id}")
  public R findHospSet(@PathVariable Long id){
    HospitalSet hospitalSet = hospitalSetService.getById(id);
    return R.ok().data("hospitalSet",hospitalSet);
  }

  //7.修改医院信息
  @ApiOperation(value = "修改医院信息")
  @PostMapping("updateHospSet")
  public R updateHospSet(@RequestBody HospitalSet hospitalSet){
    hospitalSetService.updateById(hospitalSet);
    return R.ok();
  }

  //8.批量删除
  @ApiOperation(value = "批量删除")
  @DeleteMapping("batchRemove")
  public R batchRemove(@RequestBody List<Long> idList){
    hospitalSetService.removeByIds(idList);
    return R.ok();
  }

  //9.医院设置锁定和解锁
  @ApiOperation(value = "医院设置锁定和解锁")
  @PutMapping("lockHospitalSet/{id}/{status}")
  public R lockHospitalSet(@PathVariable Long id,@PathVariable Integer status){
    HospitalSet hospitalSet = hospitalSetService.getById(id);
    hospitalSet.setStatus(status);
    hospitalSetService.updateById(hospitalSet);
    return R.ok();
  }
}

